#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;
 
struct node {
    int w, v;
    node(int v, int w) {
        this->w = w;
        this->v = v;
    }
};

bool vis[10005];
queue<int> q;
vector<node> G[10005];
int tx[10005], dis[10005];
int n, d;
vector<pair<int, int> > h;
 
bool spfa(int s) {
    memset(dis, 0x3f, sizeof(dis));
    memset(tx, 0, sizeof(tx));
    memset(vis, 0, sizeof(vis));
    dis[s] = 0;
    q.push(s);
    tx[s] = 1;
    vis[s] = 1;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(unsigned i = 0; i < G[u].size(); ++i) {
            int v = G[u][i].v;
            int z = G[u][i].w;
            if (dis[v] > dis[u] + z) {
                dis[v] = dis[u] + z;
                if (!vis[v]) {
                    if (++tx[v] > n) {
                        return 0;
                    }
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
    return 1;
}

int main() {
    int T, x;
    cin >> T;
    for (int did = 1; did <= T; ++did) {
        cin >> n >> d;
        for (int i = 1; i <= n; ++i) {
            cin >> x;
            h.push_back(make_pair(x, i));
            if (i > 1) G[i].push_back(node(i-1, -1));
        }
        
        sort(h.begin(), h.end());

		if (h[0].second > h[n-1].second) {
			reverse(h.begin(), h.end());
		}
        
        for (int i = 0; i < n - 1; ++i) {
            int a, b;
            a = max(h[i].second, h[i+1].second);
        	b = min(h[i].second, h[i+1].second);
            G[b].push_back(node(a, d));
        }
		
        cout << "Case " << did << ": ";
        if (spfa(h[0].second)) {    
            cout << dis[h[n-1].second] - dis[h[0].second] << endl;
        } else {
            cout << -1 << endl;
        }
        
        for (int i = 0; i <= n; ++i) {
        	G[i].clear();
		}
		h.clear();
    }
    
    return 0;
}


 
